home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9825 / 9825.xpi / chrome / content / related_search_results.js < prev    next >
Text File  |  2010-01-15  |  24KB  |  566 lines

  1. //related_search_queries.js
  2. (function()
  3. {
  4.     var safe_SW_getBoolPref = function(name)
  5.     {
  6.         try
  7.         {
  8.             return SW_getBoolPref(name);
  9.         }
  10.         catch(err)
  11.         {
  12.             return null;
  13.         }
  14.     };
  15.  
  16.     var getBoolPref = function(pref_name, callback)
  17.     {
  18.         callback(safe_SW_getBoolPref("extensions.smarterwiki." + pref_name));
  19.     };
  20.     if(window.chrome && window.chrome.extension) // detect chrome
  21.     {
  22.         var get_localStorage = function(key, callback) //only used for Chrome
  23.         {    
  24.             chrome.extension.sendRequest({"msg_type": "get_localStorage", "key": key}, function(response)
  25.             {
  26.                 if(callback)
  27.                 {
  28.                     callback(response.value);
  29.                 }
  30.             });
  31.         };
  32.  
  33.         getBoolPref = function(pref_name, callback)
  34.         {
  35.             get_localStorage("pref." + pref_name, function(str)
  36.             {
  37.                 callback(str == "true");
  38.             });
  39.         };
  40.         
  41.         SW_$ = $;
  42.         SW_LOG = function(){};
  43.         $.get = function(url, data, callback, type)
  44.         {
  45.             chrome.extension.sendRequest({"msg_type": "$.get", "url": url, "data": data, "type": type}, function(response)
  46.             {
  47.                 if(callback)
  48.                 {
  49.                     callback(response.data, response.textStatus);
  50.                 }
  51.             });            
  52.         };
  53.     }    
  54.     
  55.     var showOptions = function()
  56.     {
  57.         if(window.chrome && window.chrome.extension)
  58.         {
  59.             chrome.extension.sendRequest({"msg_type": "show_options"});
  60.         }
  61.         else
  62.         {
  63.             var wind = SW_showOptions();
  64.             setTimeout(function()
  65.             {
  66.                 wind.document.getElementById("smarterwikiOptions").showPane(wind.document.getElementById("SmarterWikiOptions_search_results"));
  67.             }, 150); //must wait for init
  68.         }
  69.     };
  70.  
  71.     var confirm_disable = window.confirm_disable || "Are you sure you want to disable this?\n\nTo re-enable, go to Tools -> SmarterFox";
  72.     var disable_str = window.disable_label || "disable";
  73.  
  74.     var get_log_msg_url = function(msg)
  75.     {
  76.         msg["rand"] = parseInt(Math.random() * 1000000000);
  77.         var params = [];
  78.         for(var k in msg)
  79.         {
  80.             params.push(encodeURIComponent(k) + "=" + encodeURIComponent(msg[k]));
  81.         }
  82.         /*
  83.         if("https:" == document.location.protocol)
  84.         {
  85.             return "https://ssl.msgs.smarterfox.com/log_msg?" + params.join("&");
  86.         }
  87.         */
  88.         return "http://msgs.smarterfox.com/log_msg?" + params.join("&");
  89.     };
  90.     
  91.  
  92.     var log_msg_async = function(msg, callback)
  93.     {
  94.         var $ting = $('<img style="display: none;" />');
  95.         if(callback)
  96.         {
  97.             $ting.load(callback);
  98.         }
  99.         $ting.attr("src", get_log_msg_url(msg));
  100.         return $ting;
  101.     };
  102.  
  103.     var track_click = function($a, msg)
  104.     {
  105.         $a.mouseup(function(event)
  106.         {
  107.             var original_href = $a.attr("href");
  108.             msg["redirect_to"] = original_href;
  109.             $a.attr("href", get_log_msg_url(msg));
  110.  
  111.             setTimeout(function()
  112.             {
  113.                 $a.attr("href", original_href);
  114.             }, 10);
  115.         });
  116.     };   
  117.     
  118.     function escapeHTMLEncode(str) 
  119.     {
  120.         var div = document.createElement('div');
  121.         var text = document.createTextNode(str);
  122.         div.appendChild(text);
  123.         return div.innerHTML;
  124.     }
  125.  
  126.     var truncate_string = function(s, len)
  127.     {
  128.         if(s.length > len)
  129.         {
  130.             return s.substring(0, len) + "...";
  131.         }
  132.         return s;
  133.     };
  134.  
  135.  
  136.  
  137.     var getSearchResultsURL = function(url, terms)
  138.     {
  139.         var language = navigator.language ? navigator.language : navigator.userLanguage;
  140.         var url = url.replace(/{searchTerms}/g, encodeURIComponent(terms));
  141.         url = url.replace(/{language}/g, language);
  142.         return url;
  143.     };
  144.  
  145.     var searchAmazonURL = "http://www.amazon.com/s?ie=UTF8&index=blended&field-keywords={searchTerms}&tag=smtfx1-20";
  146.     var searchOneRiotURL = "http://www.oneriot.com/search?p=smarterfox&ssrc=smarterfox_popup_bubble&spid=8493c8f1-0b5b-4116-99fd-f0bcb0a3b602&q={searchTerms}";
  147.  
  148.  
  149.     var add_oneriot_results_to_google = function(queryTerms)
  150.     {
  151.         var has_news_result = false;
  152.         $("#res ol li h3 a").each(function()
  153.         {
  154.             if(/(^| )(n|N)ews( |$)/.test($(this).text())) {
  155.                 has_news_result = true;
  156.                 return false;
  157.             }
  158.         });
  159.         if(queryTerms.length < 5 || !/^en/.test(navigator.language) || !has_news_result)
  160.         {
  161.             return;
  162.         }
  163.         SW_$.get("http://api3.smarterfox.com/api/search_oneriot", {"q": queryTerms, "limit": 1}, function(data)
  164.         {
  165.             var search_result = data["resultList"][0];
  166.             var total_shares = 0;
  167.             for(var i = 0; i < search_result["sharesList"].length; i++)
  168.             {
  169.                 total_shares += search_result["sharesList"][i]["total"];
  170.             }
  171.             if(total_shares >= 45)// && (new Date() - search_result["firstShare"]["time"] * 1000 > 5 * 24 * 60 * 60 * 1000)
  172.             {
  173.                 var displayed_url = new RegExp("https?://(.*)$").exec(search_result["displayUrl"])[1];
  174.                 displayed_url = truncate_string(displayed_url, 65);
  175.  
  176.                 var $related_search_results = jQuery('' + 
  177. '<li id="related-oneriot-search-results-google" class="related-search-results related-oneriot-search-results related-search-results-google">' + 
  178. '    <h3 class="related-search-results-title">' + 
  179. '        <span class="related-search-results-title-link">' + 
  180. '            <a class="related-search-results-title-label" href="' + getSearchResultsURL(searchOneRiotURL, queryTerms) + '">' + 
  181. '                Realtime results for <em>' + escapeHTMLEncode(queryTerms) + '</em> from OneRiot</a>' + 
  182. '            <a class="related-search-results-show-options" href="#"><img class="related-search-results-title-icon" src="http://static.smarterfox.com/media/img/smarterfox-logo.png" /></a>' + 
  183. '        </span>' + 
  184. '    </h3>' + 
  185. '    <div class="related-search-results-result">' + 
  186. '        <div class="related-search-results-result-preview">' + 
  187. '        </div>' + 
  188. '        <div class="related-search-results-result-details">' + 
  189. '            <div class="related-search-results-result-title"><a class="related-search-results-result-title-link related-search-results-oneriot-result-title-link" href="' + search_result["displayUrl"] + '">' + 
  190.                  search_result["title"] + 
  191. '            </a></div>' + 
  192. '            <div class="related-search-results-result-snippet">' + 
  193.                  search_result["snippet"] + 
  194. '            </div>' + 
  195. '            <div class="related-search-results-result-url">' + 
  196. '                <cite>' + 
  197.                      displayed_url + 
  198. '                </cite>' + 
  199. '            </div>' +
  200. '        </div>' + 
  201. '    </div>' + 
  202. '    <div class="related-search-results-bottom">' + 
  203. //'        <div class="related-search-results-remove">' +     
  204. //'        </div>' +     
  205. '        <div class="related-search-results-more">' +     
  206. '            <a class="related-search-results-more-results-link" href="' + getSearchResultsURL(searchOneRiotURL, queryTerms) + '">' + 
  207. '                <img src="http://static.smarterfox.com/media/popup_bubble/oneriot-favicon.ico" />' + 
  208. '                <span class="related-search-results-more-results-label">' + data["totalResults"] + ' more results »</span>' + 
  209. '            </a>' +     
  210. '        </div>' +     
  211. '    </div>' +     
  212. '</li>');
  213.                 var google_results = $("#res ol li");
  214.                 $(google_results[0]).before($related_search_results);
  215.                 $related_search_results.find(".related-search-results-result-title-link.related-search-results-oneriot-result-title-link")
  216.                         .mouseup(function(event)
  217.                 {
  218.                     var $a = $(this);
  219.                     var original_href = $a.attr("href");
  220.                     var msg = {
  221.                         "name": "related_search_result_clicked", 
  222.                         "source": "Google Search", 
  223.                         "type": "OneRiot", 
  224.                         "queryTerms": queryTerms,
  225.                         "action": "result #0 clicked"
  226.                         };
  227.                     msg["redirect_to"] = search_result["redirectUrl"];
  228.                     $a.attr("href", get_log_msg_url(msg));
  229.  
  230.                     setTimeout(function()
  231.                     {
  232.                         $a.attr("href", original_href);
  233.                     }, 10);
  234.                 });
  235.                 track_click($related_search_results.find(".related-search-results-title-label"), {
  236.                     "name": "related_search_result_clicked", 
  237.                     "source": "Google Search", 
  238.                     "type": "OneRiot", 
  239.                     "queryTerms": queryTerms, 
  240.                     "action": "re-searched"});
  241.  
  242.                 $related_search_results.find(".related-search-results-show-options").click(function()
  243.                 {
  244.                     showOptions();
  245.                 });
  246.             }
  247.         }, "json");
  248.     };
  249.     
  250.     var add_amazon_results_to_google = function(queryTerms)
  251.     {
  252.         add_amazon_results(queryTerms, "google", "#res ol li h3 a", function($related_search_results)
  253.         {
  254.             var google_results = $("#res ol li");
  255.             $(google_results[0]).before($related_search_results);
  256.         }, "em");
  257.     };
  258.  
  259.     var add_amazon_results_to_yahoo = function(queryTerms)
  260.     {
  261.         add_amazon_results(queryTerms, "yahoo", "#web ol li h3 a", function($related_search_results)
  262.         {
  263.             var yahoo_results = $("#web ol li");
  264.             $(yahoo_results[0]).before($related_search_results);
  265.         }, "b");
  266.     };
  267.  
  268.     var add_amazon_results_to_bing = function(queryTerms)
  269.     {
  270.         add_amazon_results(queryTerms, "bing", "#results ul li h3 a, .sr_dcard a", function($related_search_results)
  271.         {
  272.             if(queryTerms == "Amazon" || queryTerms == "amazon")
  273.             {
  274.                 $(".sr_dcard").before($related_search_results);
  275.             }
  276.             else
  277.             {
  278.                 var bing_results = $("#results ul li");
  279.                 $(bing_results[0]).before($related_search_results);
  280.             }
  281.         }, "strong");
  282.     };
  283.  
  284.     var add_amazon_results = function(queryTerms, search_engine_name, search_result_a_patt, add_div, bold_tag)
  285.     {
  286.         var has_amazon_result = false;
  287.         $(search_result_a_patt).each(function()
  288.         {
  289.             if(/www\.amazon\.com/.test($(this).attr("href"))) {
  290.                 has_amazon_result = true;
  291.                 return false;
  292.             }
  293.         });//new RegExp("^http://www.amazon.com/$"), "http://www.amazon.com/?tag=smtfx1-20"
  294.         if(has_amazon_result)
  295.         {
  296.             var title_url = getSearchResultsURL(searchAmazonURL, queryTerms);
  297.             var title_text = 'Amazon results for <em>' + escapeHTMLEncode(queryTerms) + '</em>';
  298.             if(/(A|a)mazon(.com)?/.test(queryTerms))
  299.             {
  300.                 title_url = "http://www.amazon.com/?tag=smtfx1-20";
  301.                 title_text = "<"+bold_tag+">Amazon</"+bold_tag+">.com: Online Shopping for Electronics, Apparel, Computers <b>...</b>";
  302.             }
  303.  
  304.             var $related_search_results = jQuery('' + 
  305. '<li id="related-amazon-search-results-' + search_engine_name + '" class="res related-search-results related-amazon-search-results related-search-results-' + search_engine_name + '">' + 
  306. '    <h3 class="related-search-results-title">' + 
  307. '        <span class="related-search-results-title-link">' + 
  308. '            <a class="related-search-results-title-label" href="' + title_url + '">' + 
  309. '                ' + title_text + '</a>' + 
  310. '            <a class="related-search-results-show-options" href="#"><img class="related-search-results-title-icon" src="http://static.smarterfox.com/media/img/smarterfox-logo.png" /></a>' + 
  311. '        </span>' + 
  312. '    </h3>' + 
  313. '    <div class="related-search-results-results-list">' + 
  314. '    </div>' + 
  315. '    <div class="related-search-results-bottom">' + 
  316. '        <div class="related-search-results-remove">' +     
  317. '        </div>' +     
  318. '    </div>' +     
  319. '</li>');
  320.             
  321.             add_div($related_search_results);
  322.             //$related_search_results.find(".related-search-results-results-list").slideDown("slow");
  323.  
  324.             track_click($related_search_results.find(".related-search-results-title-label"), {
  325.                 "name": "related_search_result_clicked", 
  326.                 "source": "Google Search", 
  327.                 "type": "Amazon", 
  328.                 "queryTerms": queryTerms, 
  329.                 "action": "re-searched"});
  330.  
  331.             $related_search_results.find(".related-search-results-show-options").click(function()
  332.             {
  333.                 showOptions();
  334.             });
  335.  
  336.             SW_$.get("http://api.smarterfox.com/api/search_amazon", {"q": queryTerms}, function(data)
  337.             {
  338.                 $related_search_results.find(".related-search-results-bottom").append('' + 
  339. '        <div class="related-search-results-more">' +     
  340. '            <a class="related-search-results-more-results-link" href="' + getSearchResultsURL(searchAmazonURL, queryTerms) + '">' + 
  341. '                <img class="related-search-results-favicon" src="http://www.amazon.com/favicon.ico" />' + 
  342. '                <span class="related-search-results-more-results-label">' + data["TotalResults"] + ' more results »</span>' + 
  343. '            </a>' +     
  344. '        </div>');
  345.                 
  346.                 for(var x = 0; x < Math.min(data["Items"].length, 4); x++)
  347.                 {
  348.                     $related_search_results.find(".related-search-results-results-list").append('' + 
  349. '    <div class="related-search-results-result">' + 
  350. '        <div class="related-search-results-result-preview">' + 
  351. '        </div>' + 
  352. '        <div class="related-search-results-result-details">' + 
  353. '            <img class="related-search-results-result-rating" alt="" src="http://g-ecx.images-amazon.com/images/G/01/x-locale/common/customer-reviews/ratings/stars-' + data["Items"][x]["AverageRating"].replace(".", "-") + '.gif" />' + 
  354. '<span class="related-search-results-result-title"><a class="related-search-results-result-title-link related-search-results-amazon-result-title-link" href="' + data["Items"][x]["DetailPageURL"] + '">' + 
  355.              escapeHTMLEncode(truncate_string(data["Items"][x]["Title"], 65)).replace(new RegExp("("+queryTerms+")", "gi"), '<'+bold_tag+'>$1</'+bold_tag+'>') + '</a></span>' + 
  356. '<span class="related-search-results-result-price">' + 
  357.              data["Items"][x]["FormattedPrice"] + 
  358. '</span>' +
  359. '        </div>' + 
  360. '    </div>');
  361.                 }
  362.                 $related_search_results.find(".related-search-results-result-title-link").each(function(i)
  363.                 {
  364.                     track_click($(this), {
  365.                         "name": "related_search_result_clicked", 
  366.                         "source": search_engine_name + " Search", 
  367.                         "type": "Amazon", 
  368.                         "queryTerms": queryTerms,
  369.                         "action": "result #" + i + " clicked"});
  370.                 });
  371.             }, "json");
  372.         }
  373.     };
  374.     
  375.  
  376.     var add_search_refinements_to_google = function(queryTerms)
  377.     {
  378.         add_search_refinements(queryTerms, "google", function($related_search_results)
  379.         {
  380.             var google_results = $("#res ol li");
  381.             $(google_results[0]).before($related_search_results);
  382.         }, "em");
  383.     };
  384.  
  385.     var add_search_refinements_to_yahoo = function(queryTerms)
  386.     {
  387.         add_search_refinements(queryTerms, "yahoo", function($related_search_results)
  388.         {
  389.             var yahoo_results = $("#web ol li");
  390.             $(yahoo_results[0]).before($related_search_results);
  391.         }, "b");
  392.     };
  393.  
  394.     var add_search_refinements_to_bing = function(queryTerms)
  395.     {
  396.         add_search_refinements(queryTerms, "bing", function($related_search_results)
  397.         {
  398.             var bing_results = $("#results ul li");
  399.             $(bing_results[0]).before($related_search_results);
  400.         }, "strong");
  401.     };
  402.  
  403.     var add_search_refinements = function(queryTerms, search_engine_name, add_div, bold_tag)
  404.     {
  405.         if($("#scTopOfPageRefinementLinks").length == 0)
  406.         {
  407.             var partner = { 
  408.                 uiLabel: 'FastestFox Refinements',
  409.                 partnerCode: 'fastestfox', 
  410.                 authCode: 'knm75903'
  411.             };        
  412.             SW_$.get('http://' + partner.authCode + '.surfcanyon.com/queryReformulation', 
  413.                         {"partner": partner.partnerCode,
  414.                          "authCode": partner.authCode,
  415.                          "q": queryTerms.replace(/ /g, '+')}, function(data)
  416.             {
  417.                 if($("#scTopOfPageRefinementLinks").length != 0)
  418.                 {
  419.                     return;
  420.                 }
  421.                 var items = [];
  422.                 var total_length = 0;
  423.                 $("refinement", data).each(function(i)
  424.                 {
  425.                     var query = this.textContent.toLowerCase();
  426.                     var words = queryTerms.replace(/"'\(\),/g, '').replace(/\+/g, ' ').split(' '); //"
  427.                     for (var i = 0; i < words.length; i++) 
  428.                     {
  429.                         var word = words[i];
  430.                         var containsSubstring = function(a, b)
  431.                         {
  432.                             return a.indexOf(b) != -1;
  433.                         };
  434.                         if (!containsSubstring(query.toLowerCase(), word.toLowerCase())) {
  435.                             query = query + ' ' + word;
  436.                         }
  437.                     }
  438.                     total_length += this.textContent.toLowerCase().length;
  439.                     if(total_length > 80)
  440.                     {
  441.                         return false;
  442.                     }
  443.                     items.push('<a href="http://search.surfcanyon.com/search?f=nrl' + i + '&q=' + query.replace(/ /g, '+')
  444.                                 + '&partner=' + partner.partnerCode + '">' + this.textContent.toLowerCase() + '</a>');
  445.                                 
  446.                 });
  447.                 if(items.length == 0)
  448.                 {
  449.                     return;
  450.                 }
  451.                 var code = '<div id="scTopOfPageRefinementLinks" style="height: 20px; margin-top: 7px; margin-bottom: 7px;" partner="fastestfox" sctoppos="1">' + 
  452.                 '<font size=-1><b>' + items.join('  ') + '</b>  <font size =-1 color=green>' + partner.uiLabel +
  453.                 ' <a id="disableRefinements" style="text-decoration: none; color: green;" title="Disable" href="#">[x]</a></font></font></div>';//
  454.                 var $refinements = $(code);
  455.                 $refinements.find("#disableRefinements").click(function(event)
  456.                 {
  457.                     if(doc.defaultView.confirm(confirm_disable))
  458.                     { 
  459.                         SW_setBoolPref("extensions.smarterwiki.add_search_refinements", false);
  460.                         $refinements.slideUp("normal", function()
  461.                             {
  462.                                 $refinements.remove();
  463.                             });
  464.                     }
  465.                     return false;
  466.                 });
  467.                 add_div($refinements);
  468.                 $refinements.find("a").each(function(i)
  469.                 {
  470.                     track_click($(this), {
  471.                         "name": "search_refinement_clicked", 
  472.                         "source": search_engine_name + " Search", 
  473.                         "type": "Surf Canyon", 
  474.                         "queryTerms": queryTerms,
  475.                         "action": "result #" + i + " clicked"});
  476.                 });
  477.             }, "xml");
  478.         } 
  479.     };
  480.  
  481.     var add_related_search_results = function(doc)
  482.     {
  483.         var googleURLRegExp = new RegExp("^http://www.google.(?:[^/]*)/(?:search\\?|#)(?:.*&)?q=([^&=]*)(.*)$");
  484.         var match = googleURLRegExp.exec(doc.location.href);
  485.         if(match)
  486.         {
  487.             var queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  488.             getBoolPref("add_related_search_results_oneriot", function(pref_value)
  489.             {
  490.                 if(pref_value) {
  491.                     add_oneriot_results_to_google(queryTerms);
  492.                 }
  493.             });
  494.             getBoolPref("add_related_search_results_amazon", function(pref_value)
  495.             {
  496.                 if(pref_value) {
  497.                     add_amazon_results_to_google(queryTerms);
  498.                 }
  499.             });
  500.             getBoolPref("add_search_refinements", function(pref_value)
  501.             {
  502.                 if(pref_value) {
  503.                     add_search_refinements_to_google(queryTerms);
  504.                 }
  505.             });
  506.         }
  507.  
  508.         var yahooURLRegExp = new RegExp("^http://search.yahoo.com/search[^?]*\\?(?:.*&)?p=([^&=]*)(.*)$");
  509.         match = yahooURLRegExp.exec(doc.location.href);
  510.         if(match)
  511.         {
  512.             queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  513.             getBoolPref("add_related_search_results_amazon", function(pref_value)
  514.             {
  515.                 if(pref_value) {
  516.                     add_amazon_results_to_yahoo(queryTerms);
  517.                 }
  518.             });
  519.             getBoolPref("add_search_refinements", function(pref_value)
  520.             {
  521.                 if(pref_value) {
  522.                     add_search_refinements_to_yahoo(queryTerms);
  523.                 }
  524.             });
  525.         }
  526.         
  527.         var bingURLRegExp = new RegExp("^http://www.bing.com/search[^?]*\\?(?:.*&)?q=([^&=]*)(.*)$");
  528.         match = bingURLRegExp.exec(doc.location.href);
  529.         if(match)
  530.         {
  531.             queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  532.             getBoolPref("add_related_search_results_amazon", function(pref_value)
  533.             {
  534.                 if(pref_value) {
  535.                     add_amazon_results_to_bing(queryTerms);
  536.                 }
  537.             });
  538.             getBoolPref("add_search_refinements", function(pref_value)
  539.             {
  540.                 if(pref_value) {
  541.                     add_search_refinements_to_bing(queryTerms);
  542.                 }
  543.             });
  544.         }
  545.  
  546.         /*
  547.         var baiduURLRegExp = new RegExp("^http://www.baidu.com/s[^?]*\\?(?:.*&)?wd=([^&=]*)(.*)$");
  548.         match = baiduURLRegExp.exec(doc.location.href);
  549.         if(match)
  550.         {
  551.             queryTerms = decodeURIComponent(match[1]).replace(/\+/g, " ");
  552.         }
  553.         */        
  554.      };
  555.     
  556.     
  557.     
  558.  
  559.  
  560.     var doc = window.document;
  561.     add_related_search_results(doc);  
  562.     //$(document).ready(function()
  563.     //if(SW_getBoolPref("extensions.smarterwiki.add_related_search_results"))
  564. //});    
  565. }());    
  566.